package com.java.controller;

import com.java.bean.BootStrapTableExpress;
import com.java.bean.Express;
import com.java.bean.Message;
import com.java.bean.User;
import com.java.mvc.ResponseBody;
import com.java.mvc.ResponseView;
import com.java.service.ExpressService;
import com.java.util.DateFormatUtil;
import com.java.util.JSONUtil;
import com.java.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class QRCodeController {

    @ResponseView("/wx/createQrCode.do")
    public String createQrcode(HttpServletRequest request, HttpServletResponse response) {
        Message msg = new Message();

        String code = request.getParameter("code");
        String type = request.getParameter("type");
        String userPhone = null;
        String qrCodeContent = null;
        if("express".equals(type)) {
            //快递二维码--展示单个快递的信息
            qrCodeContent = "express_" + code;
        } else {
            //用户二维码--展示用户所有快递信息
            User wxUser = UserUtil.getWxUser(request.getSession());
            userPhone = wxUser.getPhone();
            qrCodeContent = "userPhone_"+userPhone;
        }
        HttpSession session = request.getSession();
        session.setAttribute("qrcode", qrCodeContent);
        return "/wx_express/personQRcode.html";
    }

    @ResponseBody("/wx/qrcode.do")
    public String getQRCode(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        Object qrcode = session.getAttribute("qrcode");
        Message msg = new Message();
        if(qrcode == null) {
            msg.setStatus(-1);
            msg.setResult("取件码获取出错，请用户重新操作");
        } else {
            msg.setStatus(0);
            msg.setResult((String) qrcode);
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest request, HttpServletResponse response) {
        Message msg = new Message();
        String code = request.getParameter("code");
        if(ExpressService.updateStatus(code)) {
            msg.setStatus(0);
            msg.setResult("取件成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("取件码不存在，请更新二维码后重试");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest request, HttpServletResponse response) {
        Message msg = new Message();
        String code = request.getParameter("code");
        Express e = ExpressService.findByCode(code);

        if(e != null) {
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),
                    e.getCompany(),code,
                    DateFormatUtil.format(e.getInTime()),
                    e.getOutTime() == null ? "未出库": (DateFormatUtil.format(e.getOutTime())),
                    e.getStatus() == 0 ? "未取件":"已取件",e.getSysPhone());
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e2);
        } else {
            msg.setStatus(-1);
            msg.setResult("取件码不存在");
        }
        return JSONUtil.toJSON(msg);
    }
}
